package com.example.lotterysystem.controller;

import com.example.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.example.lotterysystem.common.exception.ControllerException;
import com.example.lotterysystem.common.result.CommonResult;
import com.example.lotterysystem.common.utils.JacksonUtil;
import com.example.lotterysystem.controller.param.DrawPrizeParam;
import com.example.lotterysystem.controller.param.ShowWinningResultParam;
import com.example.lotterysystem.controller.result.ShowWinningResultResult;
import com.example.lotterysystem.service.DrawPrizeService;
import com.example.lotterysystem.service.dto.ShowWinningResultDTO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class DrawPrizeController {
    @Autowired
    private DrawPrizeService drawPrizeService;

    /**
     * 保存中奖相关信息
     * @param param
     * @return
     */
    @RequestMapping("/draw-prize")
    public CommonResult<Boolean> drawPrize(@Valid @RequestBody DrawPrizeParam param) {
        // 日志打印
        log.info("drawPrize 接收到参数 DrawPrizeParam: {}",
                JacksonUtil.writeValueAsString(param));
        // 业务逻辑处理
        drawPrizeService.drawPrize(param); // 异步处理
        // 构造响应并返回
        return CommonResult.success(true);
    }

    @RequestMapping("/winning-records/show")
    public CommonResult<List<ShowWinningResultResult>> showWinningRecords(
            @Valid @RequestBody ShowWinningResultParam param) {
        // 日志打印
        log.info("showWinningRecords 接收到参数 ShowWinningResultParam: {}",
                JacksonUtil.writeValueAsString(param));
        // 业务逻辑处理
        List<ShowWinningResultDTO> showWinningResultDTOList = drawPrizeService.showWinningResult(param);
        // 构造响应并返回
        return CommonResult.success(
                convertToShowWinningResultResult(showWinningResultDTOList));
    }

    /**
     * List<ShowWinningResultResult> -> List<ShowWinningResultDTO>
     * @param showWinningResultDTOList
     * @return
     */
    private List<ShowWinningResultResult> convertToShowWinningResultResult(List<ShowWinningResultDTO> showWinningResultDTOList) {
        if (CollectionUtils.isEmpty(showWinningResultDTOList)) {
           return Arrays.asList();
        }
        List<ShowWinningResultResult> showWinningResultResultList = showWinningResultDTOList.stream()
                .map(showWinningResultDTO -> {
                    ShowWinningResultResult showWinningResultResult = new ShowWinningResultResult();
                    showWinningResultResult.setWinnerId(showWinningResultDTO.getWinnerId());
                    showWinningResultResult.setWinnerName(showWinningResultDTO.getWinnerName());
                    showWinningResultResult.setPrizeName(showWinningResultDTO.getPrizeName());
                    showWinningResultResult.setPrizeTier(showWinningResultDTO.getPrizeTier().getMessage());
                    showWinningResultResult.setWinningTime(showWinningResultDTO.getWinningTime());
                    return showWinningResultResult;
                }).collect(Collectors.toList());
        return showWinningResultResultList;
    }
}
